import { formatDate, getDays } from './funs'

class FilterPlugin {
  public install (Vue, options) {
    Vue.filter('avatarUrl', this.avatarUrl)
    Vue.filter('imgUrl', this.imgUrl)
    Vue.filter('storeUrl', this.storeUrl)
    Vue.filter('date', this.date)
    Vue.filter('ellipsis', this.ellipsis)
    Vue.filter('stringTaskStatus', this.stringTaskStatus)
    Vue.filter('tagtypebystatus', this.tagtypebystatus)
    Vue.filter('deadLineToString', this.deadLineToString)
    Vue.filter('job', this.job)
  }
  public avatarUrl (value: string) {
    if (!value || value === null || value === '') {
      return process.env.VUE_APP_CDN + 'default-avatar.jpg'
    }

    if (value.indexOf('://') !== -1) {
      return value
    }

    value = value[0] === '/' || value[0] === '\\' ? value.substring(1) : value

    return process.env.VUE_APP_CDN + value
  }
  public imgUrl (value: string) {
    if (!value || value === null) {
      return
    }

    if (value.indexOf('://') !== -1) {
      return value
    }

    value = value[0] === '/' || value[0] === '\\' ? value.substring(1) : value

    return process.env.VUE_APP_CDN + value
  }

  public storeUrl (value: string) {
    if (!value || value === null) {
      return
    }

    if (value.indexOf('://') !== -1) {
      return value
    }

    value = value[0] === '/' || value[0] === '\\' ? value.substring(1) : value

    return process.env.VUE_APP_STORE + value
  }

  public date (val, format) {
    var date = new Date(val)
    return formatDate(date, format)
  }

  public ellipsis (value, len) {
    if (!value) return ''
    if (value.length > len) {
      return value.slice(0, len) + '...'
    }
    return value
  }

  public stringTaskStatus (value) {
    switch (value) {
      case 1:
        return '学习中'
      case 2:
        return '已完成'
      case 3:
        return '已过期'
      case 4:
        return '待审核'
      case 5:
        return '未批准'
      case 6:
        return '未打分'
      case 7:
        return '待复审'
    }
  }

  public job (value) {
    switch (value) {
      case 0:
        return '营销'
      case 1:
        return '工程'
    }
  }

  public tagtypebystatus (value) {
    switch (value) {
      case 1:
        return '#007bff'
      case 2:
        return '#28a745'
      case 3:
        return '#dc3545'
      case 4:
        return '#6c757d'
      case 5:
        return '#6c757d'
      case 6:
        return '#6c757d'
    }
  }

  public deadLineToString (value, item) {
    if (item.status === 1) {
      return '剩' + getDays(new Date(), new Date(value)) + '天'
    }
    if (item.status === 2) {
      return '提前' + getDays(new Date(item.completeTime), new Date(value)) + '天'
    }
    return formatDate(new Date(value), 'yyyy-MM-dd')
  }
}

export default new FilterPlugin()
